import Vue from 'vue'
export default {
  /**
   * @purpose 获取不同状态订单数量
   * @param commit
   * @param success
   */
  getOrderTotalNumByStatus: ({commit, state}, success) => {
    if (!state.userInfo.uuid) {
      return false
    }
    let $vue = new Vue()
    $vue
      .$api
      .order
      .total()
      .then(r => {
        if (typeof success === 'function') {
          success(r)
        }
        commit('setOrderTotalNumByStatus', r)
      })
  },
  /**
   * @purpose 获取用户信息
   * @param commit
   * @param success
   */
  getUserInfo: ({commit, dispatch}, success) => {
    let $vue = new Vue()
    let token = $vue.getToken()
    if (!token || token === 'undefined') {
      return false
    }
    let autoLoading = setTimeout(() => {
      $vue.showLoading()
    }, 1000)
    $vue
      .$api
      .member
      .info()
      .then(r => {
        clearTimeout(autoLoading)
        commit('setUserInfo', r)
        if (typeof success === 'function') {
          success(r)
        }
      })
      .catch(e => {
        clearTimeout(autoLoading)
        $vue.closeLoading()
      })
  },
  /**
   * @purpose 清除登录信息
   * @param commit
   */
  clearLoginStatus: ({commit}) => {
    let $vue = new Vue()
    commit('setUserInfo', {})
    commit('setOrderTotalNumByStatus', {
      success: 0,
      wait: 0
    })
    $vue.removeToken()
  },
  /**
   * @purpose 通过地理位置获取位置信息
   * @param commit
   * @param param
   */
  getLocationByArea ({commit}, param) {
    let $vue = new Vue()
    let lngLat = param.split(',')
    $vue
      .$api
      .map
      .getLocationByLatLng(param)
      .then(r => {
        let location = {
          city: (r.addressComponent.city instanceof Array) ? r.addressComponent.province : r.addressComponent.city,
          province: r.addressComponent.province,
          district: r.addressComponent.district,
          area_code: Number(r.addressComponent.adcode),
          address: r.formatted_address,
          longitude: lngLat[0],
          latitude: lngLat[1]
        }
        commit('setLocation', location)
      })
  },
  /**
   * @purpose 获取地理位置
   * @param commit
   * @param dispatch
   * @returns {Promise<any>}
   */
  getLocation ({commit, dispatch}) {
    let $vue = new Vue()
    $vue.showLoading()
    return new Promise((resolve, reject) => {
      dispatch('wechat/sdk')
        .then(({getLocation}) => {
          getLocation({
            type: 'gcj02',
            success: res => {
              $vue
                .$api
                .map
                .getLocationByLatLng(`${res.longitude},${res.latitude}`)
                .then(r => {
                  let location = {
                    city: (r.addressComponent.city instanceof Array) ? r.addressComponent.province : r.addressComponent.city,
                    province: r.addressComponent.province,
                    district: r.addressComponent.district,
                    area_code: Number(r.addressComponent.adcode),
                    address: r.formatted_address,
                    longitude: res.longitude,
                    latitude: res.latitude
                  }
                  resolve(location)
                  commit('setLocation', location)
                })
                .catch(e => {
                  this.toast('位置信息获取失败')
                  reject(e)
                  commit('setLocation', {
                    longitude: 104.045534,
                    latitude: 30.544796
                  })
                })
            },
            fail: e => {
              $vue
                .$api
                .map
                .getLocationByLatLng(`104.045534,30.544796`)
                .then(r => {
                  let location = {
                    city: (r.addressComponent.city instanceof Array) ? r.addressComponent.province : r.addressComponent.city,
                    province: r.addressComponent.province,
                    district: r.addressComponent.district,
                    area_code: Number(r.addressComponent.adcode),
                    address: r.formatted_address,
                    longitude: 104.045534,
                    latitude: 30.544796
                  }
                  resolve(location)
                  commit('setLocation', location)
                })
                .catch(e => {
                  reject(e)
                  commit('setLocation', {
                    longitude: 104.045534,
                    latitude: 30.544796
                  })
                })
            }
          })
        })
        .catch(e => {
          // 定位失败
          $vue.toast('位置信息获取失败')
          $vue
            .$api
            .map
            .getLocationByLatLng(`104.045534,30.544796`)
            .then(r => {
              let location = {
                city: (r.addressComponent.city instanceof Array) ? r.addressComponent.province : r.addressComponent.city,
                province: r.addressComponent.province,
                district: r.addressComponent.district,
                area_code: Number(r.addressComponent.adcode),
                address: r.formatted_address,
                longitude: 104.045534,
                latitude: 30.544796
              }
              resolve(location)
              commit('setLocation', location)
            })
            .catch(e => {
              reject(e)
              commit('setLocation', {
                longitude: 104.045534,
                latitude: 30.544796
              })
            })
        })
    })
  }
}
